home *** CD-ROM | disk | FTP | other *** search
/ Unreal Tournament Game Programming for Teens / UnrealTournamentGameProgrammingForTeens.iso / Chapter Files / Chapter08 / UT2004 / Ch08Area / Classes / CodePlay.uc next >
Encoding:
Text File  |  2006-11-01  |  4.5 KB  |  153 lines

  1. //=============================================================================
  2. // CodePlay.
  3. //=============================================================================
  4. class CodePlay extends Actor placeable;
  5.  
  6.   // #1 Data Members
  7.       // This is like a class
  8.       struct CodeStatus{
  9.          var string Letter;
  10.          var bool Found;
  11.          var int Times;
  12.       };
  13.       // These are public
  14.       const CODELENGTH = 8;
  15.       const LOWASCII = 97;
  16.       const HIGHASCII = 122;
  17.       const NUMOFTRIES = 50;
  18.       
  19.       // #2 Use the struct type to declare an array
  20.       var private CodeStatus AttemptedFinds[CODELENGTH];
  21.    
  22.       var private string CodeForSearch;
  23.       var private string MessageAboutCode;
  24.       var private string Report;
  25.  
  26.    //========= Interface of Code Class ================
  27.  
  28.    // #3 with PostBeginPlay, create the code
  29.       function public MakeCode(){
  30.          local int Ctr;
  31.          while (Ctr < CODELENGTH){
  32.            CodeForSearch $= Chr(GenerateRandom());
  33.            Ctr++;
  34.          }
  35.       }
  36.  
  37.  
  38.    
  39.    // #4 Provide messages for Message property
  40.       function public string ProvideCodeMessage(){
  41.          RunCodeFinder();
  42.          return CodeForSearch $ MessageAboutCode $ Report;
  43.       }
  44.  
  45.    
  46.    // Accessor
  47.       function private string ShowCode(){
  48.           return CodeForSearch;
  49.       }
  50.    
  51.    //===================================================
  52.  
  53.    function private int GenerateRandom(){
  54.          local float High, Low;
  55.          Low = LOWASCII;
  56.          High = HIGHASCII;
  57.          return Int(RandRange(Low, High));
  58.       }
  59.    
  60.    // #5 Central private function for the class
  61.       function private RunCodeFinder(){
  62.         SetGoals();
  63.         DetectLetter();
  64.         CheckForCompleteCode();
  65.         CreateReport();
  66.       }
  67.  
  68.  
  69.  
  70.  // #6 Uses Right() and Left() built-in functions
  71.    //    to sequentially retrieve letters and place
  72.    //    them in the Letter memeber of the array elements
  73.    //    Initialize all Founds with false
  74.    //    Initialize all Times to 0
  75.       function private SetGoals(){
  76.          local int Ctr;
  77.          Ctr = 0;
  78.          while(Ctr < CODELENGTH){
  79.            AttemptedFinds[Ctr].Letter
  80.                       = Right( Left(CodeForSearch, Ctr+1), 1);
  81.            AttemptedFinds[Ctr].Found = false;
  82.            AttemptedFinds[Ctr].Times = 0;
  83.            Ctr++;
  84.          }
  85.       }
  86.  
  87.  
  88.    // #7 Gather the information on the status of each
  89.    //    Attempted discovery and assign it to Report
  90.       function private CreateReport(){
  91.          local int Ctr;
  92.          Report="";
  93.          while(Ctr < CODELENGTH){
  94.            Report $= AttemptedFinds[Ctr].Letter
  95.                   $ AttemptedFinds[Ctr].Found
  96.                   $ AttemptedFinds[Ctr].Times;
  97.            Ctr++;
  98.          }
  99.       }
  100.  
  101.  // #8 Embedded while statements with a selection statment
  102.    //    While the number of tries allowed
  103.    //    While each letter in the code string
  104.    //      Compare a randomly randomly generated letter
  105.    //      with the letter in the Letter member
  106.    //     If the letters match
  107.    //      set Found to True
  108.    //      increment Times
  109.       function private DetectLetter(){
  110.         local int Ctr, Itr;
  111.         Ctr =0;
  112.         while(Ctr < NUMOFTRIES){
  113.            Itr = 0;
  114.            while(Itr < CODELENGTH){
  115.               if( AttemptedFinds[Itr].Letter == Chr(GenerateRandom() ) ){
  116.                   AttemptedFinds[Itr].Found = true;
  117.                   AttemptedFinds[Itr].Times++;
  118.                 }// end if
  119.               Itr++;
  120.            }// end inner while
  121.            Ctr++;
  122.         }// end outer while
  123.       }// end Detect
  124.  
  125.  //#9  For the length of the code string
  126.    //     Find all the letters that have been found
  127.    //     If all letters have been found, show code complete
  128.    //     If all letters have not been found, say keep looking
  129.       function private CheckForCompleteCode(){
  130.          local int Itr, Goal;
  131.          Itr = 0;
  132.          Goal = 0;
  133.          while(Itr < CODELENGTH){
  134.            if(AttemptedFinds[Itr].Found == true){
  135.              Goal++;
  136.              if(Goal == CODELENGTH){
  137.                MessageAboutCode = "Great! You found the complete code!" ;
  138.                CodeForSearch="";
  139.                MakeCode();
  140.                SetGoals();
  141.                break;
  142.              }else{
  143.                MessageAboutCode = "Still incomplete. Keep looking." ;
  144.              }// end inner if else
  145.            }// end outer if
  146.            Itr++;
  147.          }// end while
  148.       }// end Check
  149.    
  150.  
  151.  
  152.  
  153.